var dataArr =[];
var sortArr =[];
$(function(){
	//界面初始化
	initView();	
	
	//点击事件
	bindFun();
});
function initView(){
	var managerSearch ={};
	managerSearch.phName   ="";	
	managerSearch.insName  ="";
	managerSearch.status   ="";
	managerSearch.preprintedNo ="";
	managerSearch.orderId ="";
	managerSearch.policyRef ="";
	managerSearch.createDateFrom ="";
	managerSearch.createDateTo ="";
	var manager = JSON.parse(getQueryString("data"));
	if(manager){
		$('#selectForm').formEdit(manager);
		managerSearch= manager;
	}
	managerSearch.sortDir ="desc";
	managerSearch.updateDateFrom ="";
	managerSearch.updateDateTo ="";
	managerSearch.sortField ="";
	padMarketing.ajaxNormal(
			"/pad_marketing/ordermanager/search.action",
			managerSearch,
			function (data){
				dataArr = data;
				handleData(data);	
			},
			function(data){},
			true,
			true,
			"系统正在查询您名下订单，请耐心等待"
	);
}

function bindFun(){
	$("#sortData").click(function(){
		sortDate(dataArr);
	});
	
}
function search(){	
	var start =$("input[name='createDateFrom']").val();
	var end =$("input[name='createDateTo']").val();
	if(new Date(end).getTime()-new Date(start).getTime() <0){
		alertMsg("起始日期不能大于结束日期");
		return;
	}
	closeDiv('open1');
	$("#showData").html("");
	var managerSearch =$("#selectForm").serializeObject();
	managerSearch.sortDir="desc";
	padMarketing.ajaxNormal(
			"/pad_marketing/ordermanager/search.action",
			managerSearch,
			function (data){
				dataArr = data;
				handleData(data);	
			},
			function(data){},
			true,
			true,
			"系统正在查询您名下订单，请耐心等待"
	);
}
function handleData(json){
	sortArr=[];
	for(var i in json){
		var app = json[i];
		var createDate = app.createDate;
		
		sortArr[i] = createDate;
	}
	sortArr =handleDouble(sortArr);
	var html="";
	for(var i in sortArr){
		html+="<h3 class='padding-left-15 lh-45'>"+sortArr[i]+"</h3>";
		for(var j in json){
			if(json[j].createDate == sortArr[i] ){
				html+="<div><dl class='bookshelf-list span2' orderId ='"+json[j].orderId+"' status ='"+json[j].status+"'>";
				html+="<dt>";
				html+="<div class='img-box'>";		        
				html+=   "<img src=\'/portal/resources/images/product/SJ_"+ json[j].productId +".jpg\'>";
		         if(json[j].status=="F"){
		        	 html+=   "<label class='sort sort-success'><i>已支付</i></label>";
		         }else if(json[j].status=="P"){
		        	 html+=   "<label class='sort sort-danger'><i>待支付</i></label>";
		         }else if(json[j].status=="O"){		        	
		        	 html+=   "<label class='sort sort-warning'><i>人工核保</i></label>";
		         }else if(json[j].status=="C"){
		        	 html+=   "<label class='sort sort-gray'><i>已撤单</i></label>";
		         }else if(json[j].status=="I"){
		        	 html+=   "<label class='sort sort-purple'><i>录入中</i></label>";
		         }else if(json[j].status=="U"){
		        	 html+=   "<label class='sort sort-info'><i>待拍照</i></label>";
		         }else if(json[j].status=="S"){
		        	 html+=   "<label class='sort sort-primary'><i>待签名</i></label>";
		         }
		         
		         html+=   "</div>";
		         html+=    '<div class="check-list hide">';
		 	     html+=     '<input type="checkbox">'; 
		 	     html+=      '<label for="">&nbsp;</label>';
		 	     html+=    '</div>';
		         html+="</dt>";
		         html+="<dd>";
		         html+=   "<h3 class='blue b font-18 lh-45'>"+HtmlEncode(json[j].productName)+"</h3>";
		         html+=   "<div class='popover-panel-new'>";
		         if(json[j].status!="I"){
		        	 html+=   "<a onclick='lookManager(this,2)' class='text-center'>";
			         html+=   " <p class='icon icon-circle-view'></p>";
			         html+=   "  <p class='purple font-14'>只读</p>";
			         html+=   "</a>";
		         }		         	
		         if(json[j].status=="S"){
		        	 html+=   "<a  onclick='writeName(this,2)' class='text-center'>";
			         html+=   " <p class='icon icon-sign'></p>";
			         html+=   " <p class='blue-d font-14'>签名</p>";
			         html+=   "</a>";
		         } 
		         if(json[j].status=="U"){
		        	 html+=   "<a  onclick='takePhotoM(this,2)' class='text-center'>";
			         html+=   " <p class='icon icon-photo'></p>";
			         html+=   " <p class='yellow font-14'>拍照</p>";
			         html+=   "</a>";
		         } 
		         if(json[j].allowPayment=="Y"&&json[j].salesTool=="APP"){
		        	 html+=   "<a  onclick='payMoney(this,2)' class='text-center'>";
			         html+=   " <p class='icon icon-circle-pay'></p>";
			         html+=   " <p class='green font-14'>支付</p>";
			         html+=   "</a>";
		         }
		         if(json[j].allowContinue=="Y"&&json[j].salesTool=="APP"){
		        	 html+=   "<a   onclick='changeManager(this,2)' class='text-center'>";
			         html+=   " <p class='icon icon-jxtb'></p>";
			         html+=   " <p class='blue-l font-14'>继续投保</p>";
			         html+=   "</a>";
		         } 
		         if(json[j].allowDel=="Y"&&json[j].salesTool=="APP"){
		        	 html+=   "<a  onclick='deleteManager(this,2)'  class='text-center'>";
			         html+=   " <p class='icon icon-remove-sm'></p>";
			         html+=   " <p class='red font-14'>删除</p>";
			         html+=   "</a>";
		         }
		        if(json[j].allowCbi=="Y"&&json[j].salesTool=="APP"){
		        	html+=   "<a  class='text-center'>";
			        html+=   " <p class='icon icon-circle-edit sm'></p>";
			        html+=   " <p class='blue font-14'>撤单</p>";
			        html+=   "</a>";
		        }
		         html+=   "<a  onclick='copyManager(this,2)' class='text-center'>";
		         html+=   "  <p class='icon icon-circle-copy'></p>";
		         html+=   "  <p class='orange font-14'>复制</p>";
		         html+=   "</a>";
		         html+=   "</div>";	         
		         html+=   "</dd>";
		         html+=   "</dl></div>";		         
			}
		}
		html+='<div class="clearfix"></div>';
	}
	$("#showData").append(html);
	$("dl").each(function(index,me){
		var orderId = $(me).attr("orderId");
		for(var k in json){
			var app = json[k];
			if(orderId==app.orderId){
				$(me).data("data",app);
			}
		}
		
	});
	$(".bookshelf-list").popover();
	if(myScroll)
		myScroll.refresh(); 
}

function handleDouble(sortArr)
{
	var n = []; //一个新的临时数组
	for(var i in sortArr) //遍历当前数组
	{
		//如果当前数组的第i已经保存进了临时数组，那么跳过，
		//否则把当前项push到临时数组里面
		if (n.indexOf(sortArr[i]) == -1) n.push(sortArr[i]);
	}
	return n;
}
function sortDate(dataArr){
	$("#showData").html("");
	sortArr.reverse();
	var html="";
	for(var i in sortArr){
		html+="<h3 class='padding-left-15 lh-45'>"+sortArr[i]+"</h3>";
		for(var j in dataArr){
			if(dataArr[j].createDate == sortArr[i] ){
				html+="<div><dl class='bookshelf-list span2' orderId ='"+dataArr[j].orderId+"' status ='"+dataArr[j].status+"'>";
				html+="<dt>";
				html+="<div class='img-box'>";		        
				html+=   "<img src=\'/portal/resources/images/product/SJ_"+ dataArr[j].productId +".jpg\'>";
		         if(dataArr[j].status=="F"){
		        	 html+=   "<label class='sort sort-success'><i>已支付</i></label>";
		         }else if(dataArr[j].status=="P"){
		        	 html+=   "<label class='sort sort-danger'><i>待支付</i></label>";
		         }else if(dataArr[j].status=="O"){		        	
		        	 html+=   "<label class='sort sort-warning'><i>人工核保</i></label>";
		         }else if(dataArr[j].status=="C"){
		        	 html+=   "<label class='sort sort-gray'><i>已撤单</i></label>";
		         }else if(dataArr[j].status=="I"){
		        	 html+=   "<label class='sort sort-purple'><i>录入中</i></label>";
		         }else if(dataArr[j].status=="U"){
		        	 html+=   "<label class='sort sort-info'><i>待拍照</i></label>";
		         }else if(dataArr[j].status=="S"){
		        	 html+=   "<label class='sort sort-primary'><i>代签名</i></label>";
		         }
		         html+=   "</div>";
		         html+= "</dt>";
		         html+= "<dd>";
		         html+=   "<h3 class='blue b font-18 lh-45'>"+HtmlEncode(dataArr[j].productName)+"</h3>";
		         html+=   "<div class='popover-panel-new'>";
		         if(dataArr[j].status!="I"){
		        	 html+=   "<a  onclick='lookManager(this,2)' class='text-center'>";
			         html+=   " <p class='icon icon-circle-view'></p>";
			         html+=   "  <p class='purple font-14'>只读</p>";
			         html+=   "</a>";
		         }		         
		         if(dataArr[j].status=="S"&&dataArr[j].salesTool=="APP"){
		        	 html+=   "<a  onclick='writeName(this,2)' class='text-center'>";
			         html+=   " <p class='icon icon-sign'></p>";
			         html+=   " <p class='blue-d font-14'>签名</p>";
			         html+=   "</a>";
		         }if(dataArr[j].status=="U"&&dataArr[j].salesTool=="APP"){
		        	 html+=   "<a  onclick='takePhotoM(this,2)' class='text-center'>";
			         html+=   " <p class='icon icon-photo'></p>";
			         html+=   " <p class='yellow font-14'>拍照</p>";
			         html+=   "</a>";
		         }
		         if(dataArr[j].allowPayment=="Y"&&dataArr[j].salesTool=="APP"){
		        	 html+=   "<a  onclick='payMoney(this,2)' class='text-center'>";
			         html+=   " <p class='icon icon-circle-pay'></p>";
			         html+=   " <p class='green font-14'>支付</p>";
			         html+=   "</a>";
		         }
		         if(dataArr[j].allowContinue=="Y"&&dataArr[j].salesTool=="APP"){
		        	 html+=   "<a   onclick='changeManager(this,2)' class='text-center'>";
			         html+=   " <p class='icon icon-jxtb'></p>";
			         html+=   " <p class='blue-l font-14'>继续投保</p>";
			         html+=   "</a>";
		         } 
		         if(dataArr[j].allowDel=="Y"&&dataArr[j].salesTool=="APP"){
		        	 html+=   "<a onclick='deleteManager(this,2)'  class='text-center'>";
			         html+=   " <p class='icon icon-remove-sm'></p>";
			         html+=   " <p class='red font-14'>删除</p>";
			         html+=   "</a>";
		         }
		         html+=   "<a  onclick='copyManager(this,2)' class='text-center'>";
		         html+=   "  <p class='icon icon-circle-copy'></p>";
		         html+=   "  <p class='orange font-14'>复制</p>";
		         html+=   "</a>";
		         html+=   "</div>";	         
		         html+=   "</dd>";
		         html+=   "</dl></div>";		         
			}
		}
		html+='<div class="clearfix"></div>';
	}
	$("#showData").append(html);
	$("dl").each(function(index,me){
		var orderId = $(me).attr("orderId");
		for(var k in dataArr){
			var app = dataArr[k];
			if(orderId==app.orderId){
				$(me).data("data",app);
			}
		}
		
	});
	$(".bookshelf-list").popover();
	if(myScroll)
		myScroll.refresh(); 
}
function switchHtml(type){
	var managerSearch =$("#selectForm").serializeObject();
	location.href="/pad_marketing/ordermanager/"+type+".action?data="+encodeURIComponent(JSON.stringify(managerSearch));
}
function changePayMoney(me,index){
	window.location.href="/pad_marketing/main/editPay.action?url=ordermanager/"+orderManager(index)+".action";
}
function payMoneyHref(me,index){
	window.location.href="/pad_marketing/main/pay.action?url=ordermanager/"+orderManager(index)+".action";
}